﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class SystemManage_UserEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string UserCode
    {
        get
        {
            return Request.QueryString["usercode"] != null ? Request.QueryString["usercode"].ToString() : "";
        }
    }

    private DataSet UserAndGroups
    {
        get
        {
            DataSet userAndGroups = null;
            if (ViewState["UserAndGroups"] != null)
            {
                userAndGroups = (DataSet)ViewState["UserAndGroups"];
            }
            return userAndGroups;
        }
        set
        {
            ViewState["UserAndGroups"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("100400");
        
        if (!IsPostBack)
        {
            if (this.UserCode != "")
            {
                this.UserAndGroups = new BLL.USER_USERGROUP().GetGroupByUser(Request.QueryString["usercode"].ToString());
                Bind();
                BindCheckList();
            }
            else
            {
                chkUserGroup.Visible = false;
                btnUpdateUserGroup.Visible = false;
            }
            
        }
    }

    private void BindCheckList()
    {
        chkUserGroup.DataSource = new BLL.USERGROUP().GetAllList();
        chkUserGroup.DataTextField = "UG_NAME";
        chkUserGroup.DataValueField = "UG_CODE";
        chkUserGroup.DataBind();

        if (this.UserCode != "")
        {
            foreach (ListItem temp in chkUserGroup.Items)
            {
                for (int i = 0; i < this.UserAndGroups.Tables[0].Rows.Count; i++)
                {
                    if (temp.Value == this.UserAndGroups.Tables[0].Rows[i]["UUG_UGCODE"].ToString())
                    {
                        temp.Selected = true;
                    }
                }
            }
        }
    }

    

    private void Bind()
    {
        if (Request.QueryString["usercode"] != null)
        {
            Label1.Text = "编辑用户";
            btnAdd.Text = "编辑";
            Model.USERS l = new BLL.USERS().GetModel(Request.QueryString["usercode"].ToString());
            txtUserCode.Text = l.USERS_CODE;
            txtUserCode.Enabled = false;
            txtUserName.Text = l.USERS_NAME;
            txtPwd.Enabled = false;
            rbtnVIP.SelectedValue = l.VIPCO.ToString();
            rbtnTY.SelectedValue = l.USERS_STOP.ToString();

            //隐藏密码控件
        }
        else
        {
            
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Model.USERS l = new Model.USERS();
        l.USERS_CODE = txtUserCode.Text.Trim();
        l.USERS_NAME = txtUserName.Text.Trim();
        l.VIPCO = Int32.Parse(rbtnVIP.SelectedValue);
        l.USERS_STOP = Int32.Parse(rbtnTY.SelectedValue);
        l.USERS_PASSWORD = txtPwd.Text.Trim();
        if (Request.QueryString["usercode"] != null)
        {
            if (l.USERS_CODE.Trim() == "" || l.USERS_NAME.Trim() == "") { Share.Alert(Page,"请输入*号项"); return; }
            if (Check.CheckPermission("100400", "00002"))
            {

                    if (new BLL.USERS().Update(l))
                    {
                        Share.InsertLog("100400", "00002");
                        Response.Redirect("UsersList.aspx");
                    }
                    else
                    {
                        Share.Alert(this.Page, "操作失败"); return;
                    }
            }
            else
            {
                Share.Alert(this.Page,"无权操作"); return;
            }
        }
        else
        {
            if (l.USERS_CODE.Trim() == "" || l.USERS_NAME.Trim() == "" || l.USERS_PASSWORD.Trim() == "")
            {
                Share.Alert(Page,"请输入*号项"); return; 
            }
            if (Check.CheckPermission("100400", "00001"))
            {
                if (new BLL.USERS().Exists(l.USERS_CODE))
                {
                    Share.Alert(this.Page, "编号重复"); return;
                }
                else
                {
                    if (new BLL.USERS().Add(l) != null)
                    {
                        Share.InsertLog("100400", "00001");
                        Response.Redirect("UsersList.aspx");
                    }
                    else
                    {
                        Share.Alert(this.Page, "操作失败"); return;
                    }
                }
            }
            else
            {
                Share.Alert(this.Page,"无权操作"); return;
            }
        }
    }


    protected void btnUpdateUserGroup_Click(object sender, EventArgs e)
    {
        StringBuilder strSql = new StringBuilder();
        strSql.Append("delete from USER_USERGROUP where UUG_USERCODE='");
        strSql.Append(Request.QueryString["usercode"].ToString());
        strSql.Append("'|");
        foreach (ListItem temp in chkUserGroup.Items)
        {
            if (temp.Selected)
            {
                strSql.Append("insert into USER_USERGROUP(UUG_USERCODE,UUG_UGCODE) values('");
                strSql.Append(Request.QueryString["usercode"].ToString());
                strSql.Append("','");
                strSql.Append(temp.Value);
                strSql.Append("')|");
            }
        }
        string stringSql = strSql.ToString();
        char[] charToTrim = new char[] { '|' };
        string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
        if (new BLL.USER_USERGROUP().UpdateUserGroup(sqlCommandArray))
        {
            Share.Alert(this.Page, "更新用户组成功");
        }
        else 
        {
            Share.Alert(this.Page, "更新用户组失败");
        }
    }
}
